package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.MchntApexInfoMapper;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.MchntApexInfoMapperImpl;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.MchntApexInfo;
import cc.rengu.igas.bomp.common.entity.MchntBaseInfo;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.MchntApexPropeytyEnum;
import cc.rengu.igas.bomp.common.enums.MchntStatusEnum;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.common.enums.WechatActivityStatusEnum;
import cc.rengu.igas.bomp.common.util.InitParamUtil;
import cc.rengu.igas.bomp.core.realize.NormalMchntInfoService;
import cc.rengu.igas.bomp.core.realize.impl.NormalMchntInfoServiceImpl;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.MchntApexInfoBean;
import cc.rengu.igas.bomp.facade.request.WeChatActivityRateRegistQueryRequest;
import cc.rengu.igas.bomp.facade.response.WeChatActivityRateRegistQueryResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.request.ActivityRegistResultQueryRequest;
import cc.rengu.igas.channel.wechat.facade.response.ActivityRegistResultQueryResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 微信特殊费率申请单查询
 *
 * @version 1.0
 * @date 2019/08/15
 */
public class WeChatActivityRateRegistQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WeChatActivityRateRegistQueryRequest weChatActivityRateRegistQueryRequest = new WeChatActivityRateRegistQueryRequest();
        ConvertUtil.convertOutput(weChatActivityRateRegistQueryRequest);
        return weChatActivityRateRegistQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        WeChatActivityRateRegistQueryRequest weChatActivityRateRegistQueryRequest = (WeChatActivityRateRegistQueryRequest) request;
        WeChatActivityRateRegistQueryResponse weChatActivityRateRegistQueryResponse = new WeChatActivityRateRegistQueryResponse();
        BizResponse<WeChatActivityRateRegistQueryResponse> bizResponse = new BizResponse<>();
        bizResponse.setResult(weChatActivityRateRegistQueryResponse);
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        weChatActivityRateRegistQueryResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        weChatActivityRateRegistQueryResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        NormalMchntInfoService normalMchntInfoService = new NormalMchntInfoServiceImpl();
        MchntBaseInfo mchntBaseInfo = normalMchntInfoService.checkMchntStatus(weChatActivityRateRegistQueryRequest.getHeader().getInstId(), weChatActivityRateRegistQueryRequest.getMchntNo());
        mchntBaseInfo.setMchntStatus(MchntStatusEnum.UNNORMAL.getStatus());

        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo());
        if (null == wechatMchntInfo) {
            rglog.error("无微信商户信息.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "无微信商户信息");
        }

        MchntApexInfoMapper mchntApexInfoMapper = new MchntApexInfoMapperImpl();
        MchntApexInfo wActivityIdMchntSysParam = mchntApexInfoMapper.selectMchntApexInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), MchntApexPropeytyEnum.WECHAT_ACTIVITY_ID.getPropertyKey());
        if (null == wActivityIdMchntSysParam) {
            rglog.error("未申请优惠费率活动.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "未申请优惠费率活动");
        }
        MchntApexInfo wApplicationIdMchntSysParam = mchntApexInfoMapper.selectMchntApexInfoByPrimaryKey(mchntBaseInfo.getInstId(), mchntBaseInfo.getMchntNo(), MchntApexPropeytyEnum.APPLICATION_ID.getPropertyKey());
        if (null == wApplicationIdMchntSysParam) {
            rglog.error("申请单号为空.");
            throw new BizException(RespCodeEnum.WECHAT_ACTIVITY_RATE_ERROR.getRespCode(), "申请单号为空");
        }

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);
        ActivityRegistResultQueryRequest activityRegistResultQueryRequest = new ActivityRegistResultQueryRequest();
        Header header = InitParamUtil.initWechatHeader("actvregrstqry", mchntBaseInfo.getInstId(), sysSeqNum);
        activityRegistResultQueryRequest.setHeader(header);
        activityRegistResultQueryRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        activityRegistResultQueryRequest.setThirdMchntNo(wechatMchntInfo.getWechatMchntNo());
        activityRegistResultQueryRequest.setChannelType(wechatMchntInfo.getDstChannelType());
        activityRegistResultQueryRequest.setApplicationId(wApplicationIdMchntSysParam.getPropertyValue());

        DubboService dubboService = new DubboServiceImpl();
        Result result = (Result) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "activityRegistResultQuery", activityRegistResultQueryRequest);
        if (null == result || !result.isSuccess() || null == result.getResult()) {
            rglog.error("服务调用失败.");
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }

        ActivityRegistResultQueryResponse activityRegistResultQueryResponse = (ActivityRegistResultQueryResponse) result.getResult();
        if (!DubboMethodConstant.DUBBO_SUCCESS.equals(activityRegistResultQueryResponse.getRespCode())) {
            rglog.error("调用服务失败<{}>-<{}>", activityRegistResultQueryResponse.getRespCode(), activityRegistResultQueryResponse.getRespDesc());
            throw new BizException(activityRegistResultQueryResponse.getRespCode(), activityRegistResultQueryResponse.getRespDesc());
        }
        String wActivityStatus = WechatActivityStatusEnum.getEpayStatusByWechatStatus(activityRegistResultQueryResponse.getApplicationStatus());

        /*保存申请单状态*/
        List<MchntApexInfoBean> mchntApexInfoBeanList = new ArrayList<>();
        MchntApexInfoBean wApplicationIdMchntApexInfoBean = new MchntApexInfoBean();
        mchntApexInfoBeanList.add(wApplicationIdMchntApexInfoBean);
        wApplicationIdMchntApexInfoBean.setPropertyKey(MchntApexPropeytyEnum.ACTIVITY_STATUS.getPropertyKey());
        wApplicationIdMchntApexInfoBean.setPropertyValue(wActivityStatus);
        weChatActivityRateRegistQueryResponse.setActivityApplyStatus(wActivityStatus);

        if (WechatActivityStatusEnum.APPLICATION_STATE_REJECTED.getEpayStatus().equals(wActivityStatus)) {
            /*申请单拒绝原因*/
            MchntApexInfoBean wActivityReasonMchntApexInfoBean = new MchntApexInfoBean();
            mchntApexInfoBeanList.add(wActivityReasonMchntApexInfoBean);
            wActivityReasonMchntApexInfoBean.setPropertyKey(MchntApexPropeytyEnum.ACTIVITY_REASON.getPropertyKey());
            wActivityReasonMchntApexInfoBean.setPropertyValue(activityRegistResultQueryResponse.getRejectReason());
            weChatActivityRateRegistQueryResponse.setActivityApplyStatus(activityRegistResultQueryResponse.getRejectReason());
        }

        if (WechatActivityStatusEnum.APPLICATION_STATE_PASSED.getEpayStatus().equals(wActivityStatus)) {
            /*申请单通过时间*/
            MchntApexInfoBean wActivityStatusMchntApexInfoBean = new MchntApexInfoBean();
            mchntApexInfoBeanList.add(wActivityStatusMchntApexInfoBean);
            wActivityStatusMchntApexInfoBean.setPropertyKey(MchntApexPropeytyEnum.ACTIVITY_TIME.getPropertyKey());
            wActivityStatusMchntApexInfoBean.setPropertyValue(activityRegistResultQueryResponse.getPassTime());
        }

        /** 开启数据库事务 */
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        dbsUtil.dbsBeginTransaction();
        try {
            InitParamUtil.savemchntApexInfo(mchntBaseInfo, mchntApexInfoBeanList);

            /**提交事务*/
            dbsUtil.dbsEndTransaction(true);
        } catch (Exception e) {
            rglog.error(cc.rengu.utility.base.StringUtil.ExceptionToString(e));
            dbsUtil.dbsEndTransaction(false);
            throw new BizException(RespCodeEnum.SYSTEM_BUSYNESS.getRespCode(), RespCodeEnum.SYSTEM_BUSYNESS.getRespDesc());
        }

        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WeChatActivityRateRegistQueryResponse weChatActivityRateRegistQueryResponse = (WeChatActivityRateRegistQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(weChatActivityRateRegistQueryResponse);
    }
}
